package com.juc.semaphore.semaphore12;

import java.util.concurrent.Semaphore;

public class Service {
    /**
     * tryAcquire(permits) 的作用是尝试的获得x个许可 如果获取不到则返回false
     */
    private Semaphore semaphore = new Semaphore(3);

    public void testMethod() {
        if (semaphore.tryAcquire(3)) {
            System.out.println("threadName=  " + Thread.currentThread().getName() + " 首选进入！");
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                String newStr = new String();
                Math.random();
            }
            //方法release 对应的permits 也要修改
            semaphore.release(3);
        }else {
            System.out.println("threadName = " + Thread.currentThread().getName() + "未成功进入 ！");
        }
    }

}
